﻿using System;
using System.Text;
using System.Net;
using System.IO;
using System.Drawing;
using System.Web;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

namespace laicigoujiaoyi
{
    class Dog
    {
        private static string cookie;
        public static string GetCookieva
        {
            get { return cookie; }
            set { cookie = value; }
        }
        /// <summary>
        /// 刷新列表
        /// </summary>
        /// <returns></returns>
        public static string queryPetsOnSale(string pageNo, string pageSize, string querySortType)
        {
            //第几页，
            //几个一页，
            //，RAREDEGREE_DESC 稀有度高 RAREDEGREE_ASC 稀有度低编码低 ，AMOUNT_ASC价格低， AMOUNT_DESC 价格高
            //
            //pageNo: 1, pageSize: 10, querySortType: "RAREDEGREE_ASC", petIds: [], lastAmount: null,
            string data_in = "{\"pageNo\":" + (pageNo) + ",\"pageSize\":" + (pageSize) + ",\"querySortType\":\"" + (querySortType) + "\",\"petIds\":[],\"lastRareDegree\":null,\"requestId\":" + (Timenum()) + ",\"appId\":1,\"tpl\":\"\"}";
            string post_url = "https://pet-chain.baidu.com/data/market/queryPetsOnSale";
            string res_str = HttpPost(data_in, post_url);
            return res_str;
        }
        /// <summary>
        /// 狗属性
        /// </summary>
        /// <returns></returns>
        public static string queryPetById(string petId)
        {
            //   HttpWebRequest request;
            //petId = "1882327725953129096";
            string data_in = "{\"petId\":\"" + petId + "\"," +
             "\"requestId\":" + (Timenum()) + "," +
             "\"appId\":1," +
             "\"tpl\":\"" +
             "\"}";
            string post_url = "https://pet-chain.baidu.com/data/pet/queryPetById";
            string res_str = HttpPost(data_in, post_url);
            return res_str;
        }
        /// <summary>
        /// 卖出
        /// </summary>
        /// <returns></returns>
        public static string GetsalePet(string petId, string amount)
        {

            //petId = "1882327725953129096";
            string data_in = "{\"petId\":\"" + petId +
             "\",\"amount\":\"" + amount +
             "\",\"requestId\":" + (Timenum()) + ",\"appId\":1,\"tpl\":\"\"}";
            string post_url = "https://pet-chain.baidu.com/data/market/salePet";
            string res_str = HttpPost(data_in, post_url);
            return res_str;
        }
        /// <summary>
        /// 错误提示
        /// </summary>
        /// <param name="newret"></param>
        /// <returns></returns>
        public static string GeterrorMsg(string newret)
        { 
        JObject res_j = (JObject)JsonConvert.DeserializeObject(newret);
        string res_ja = (string)res_j["errorNo"];
        string res_errorMsg = (string)res_j["errorMsg"];
            return res_errorMsg;
        }
        /// <summary>
        /// 取消卖出状态
        /// </summary>
        /// <returns></returns>
        public static string GetunsalePet(string petId)
        {
            //petId = "1882327725953129096";
            string data_in = "{\"petId\":\"" + petId + "\"," +
             "\"requestId\":" + (Timenum()) + "," +
             "\"appId\":1," +
             "\"tpl\":\"" +
             "\"}";
            string post_url = "https://pet-chain.baidu.com/data/market/unsalePet";
            string res_str = HttpPost(data_in, post_url);
            return res_str;
        }
        /// <summary>
        /// 买狗
        /// </summary>
        /// <param name="petid"></param>
        /// <param name="vcode"></param>
        /// <param name="cookie"></param>
        /// <param name="price"></param>
        /// <param name="seed"></param>
        /// <param name="cha"></param>
        /// <returns></returns>
        public static string buyPet(string petid, string vcode, string price, string seed, string cha)
        {
            string data_in = "{\"petId\":\"" + petid +
                "\",\"amount\":\"" + price +
                "\",\"seed\":\"" + seed +
                "\",\"captcha\":\"" + cha +
                "\",\"validCode\":\"" + vcode +
                "\",\"requestId\":" + (Timenum ()) + ",\"appId\":1,\"tpl\":\"\"}";
             string post_url = "https://pet-chain.baidu.com/data/txn/create";
            return HttpPost( data_in, post_url);
        }
        /// <summary>
        /// wangye
        /// </summary>
        /// <param name="seed"></param>
        /// <param name="cha"></param>
        /// <param name="shareCode"></param>
        /// <param name="b"></param>
        /// <param name="c"></param>
        /// <returns></returns>
        public static string getgift(string seed, string cha, string shareCode, int b, string c)
        {
            //https://pet-chain.baidu.com/chain/splash?appId=6&tpl=baiduapp&timeStamp=1520305544304&nounce=15868&token=b9b511c1c8ffcd3a3e2abfee724c1534
            //https://mr.baidu.com/biut644  https://pet-chain.baidu.com/api/digitpet/getwechatsign?target_url=https%3A%2F%2Fpet-chain.baidu.com%2Fchain%2Fsplash%3FappId%3D6%26tpl%3Dbaiduapp
            string data_in = "{\"seed\":\"" + seed +
                "\",\"captcha\":\"" + cha +
                "\",\"shareCode\":\"" + shareCode +
                "\",\"requestId\":" + (Timenum ()) + ",\"appId\":" + (b) + ",\"tpl\":\"\"}";
            string post_url = "https://pet-chain.baidu.com/data/user/gift";
            return HttpPost( data_in, post_url);
        }
        /// <summary>
        /// 手机app
        /// </summary>
        /// <param name="seed"></param>
        /// <param name="cha"></param>
        /// <param name="shareCode"></param>
        /// <param name="b"></param>
        /// <param name="c"></param>
        /// <returns></returns>
        public static string GethasMobile(string seed, string cha, string shareCode, int b, string c)
        {
            //https://pet-chain.baidu.com/chain/splash?appId=6&tpl=baiduapp&timeStamp=1520305544304&nounce=15868&token=b9b511c1c8ffcd3a3e2abfee724c1534
            string data_in = "{\"seed\":\"" + seed +
                "\",\"captcha\":\"" + cha +
                "\",\"shareCode\":\"" + shareCode +
                "\",\"requestId\":" + (Timenum()) + ",\"appId\":" + (b) + ",\"tpl\":" + (c) + "\"\"}";
            string post_url = "https://pet-chain.baidu.com/data/user/hasMobile";
            return HttpPost(data_in, post_url);
        }


        /// <summary>
        /// 获取验证码
        /// </summary>
        /// <returns></returns>
        public static string getCha()
        {
            string data_in = "{\"requestId\":" + (Timenum ()) + ",\"appId\":1,\"tpl\":\"\"}";
            string post_url = "https://pet-chain.baidu.com/data/captcha/gen";
            return HttpPost( data_in, post_url);
        }
        public static Image str2Image(string str)
        {
            byte[] b = Convert.FromBase64String(str);
            MemoryStream ms = new MemoryStream(b);
            Bitmap bitmap = new Bitmap(ms);
            return bitmap;
        }
        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="cookie"></param>
        /// <returns></returns>
        public static string showUser()
        {
            string data_in = "{\"requestId\":" + (Timenum()) + ",\"appId\":2,\"tpl\":\"\"}";
            string post_url = "https://pet-chain.baidu.com/data/user/get";
            return HttpPost( data_in, post_url);
        }
        /// <summary>
        /// 获取订单信息
        /// </summary>
        /// <param name="cookie"></param>
        /// <returns></returns>
        public static string getMyOrder()
        {
            string data_in = "{\"pageNo\":1,\"pageSize\":20,\"pageTotal\":-1,\"requestId\":" + (Timenum()) + ",\"appId\":1,\"tpl\":\"\"}";
            string post_url = "https://pet-chain.baidu.com/data/user/order/list";
            string res_str= HttpPost( data_in, post_url);
            return res_str;
        }
        /// <summary>
        /// 获取自己的狗狗
        /// </summary>
        /// <returns></returns>
        public static string GetMyDoglist()
        {
            string data_in = "{\"pageNo\":1,\"pageSize\":20,\"pageTotal\":-1,\"requestId\":" + (Timenum()) + ",\"appId\":1,\"tpl\":\"\"}";
            string post_url = "https://pet-chain.baidu.com/data/user/pet/list";
            string res_str = HttpPost(data_in, post_url);
            return res_str;
        }
        /// <summary>
        /// POst
        /// </summary>
        /// <param name="cookie"></param>
        /// <param name="data_in"></param>
        /// <param name="post_url"></param>
        /// <returns></returns>
        public static string HttpPost( string data_in, string post_url)
        {
            HttpWebRequest request;
            request = (HttpWebRequest)WebRequest.Create(post_url);
            request.Method = "POST";
            request.ContentType = "application/json";

            request.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.56 Safari/536.5";

            request.CookieContainer = new CookieContainer();
            request.CookieContainer.Add(new Cookie("BDUSS", cookie, "/", ".baidu.com"));

            request.ContentLength = Encoding.UTF8.GetByteCount(data_in);
            byte[] data = (Encoding.GetEncoding("utf-8")).GetBytes(data_in);
            request.GetRequestStream().Write(data, 0, data.Length);
            HttpWebResponse res = (HttpWebResponse)request.GetResponse();
            Stream st = res.GetResponseStream();
            Encoding en = Encoding.GetEncoding("utf-8");
            StreamReader st_r = new StreamReader(st, en);
            string res_str = st_r.ReadToEnd();

          

            string a=  res.Cookies.Count.ToString();
            res.Close();
            st.Close();
            st_r.Close();
            return res_str;
        }
        public void Getdeletecookie()
        {
            //HttpCookie aCookie;
            //string cookieName;
            //int limit = res.Cookies.Count;
            //for (int i = 0; i < limit; i++)
            //{
            //    cookieName = res.Cookies[i].Name;
            //    aCookie = new HttpCookie(cookieName);
            //    aCookie.Expires = DateTime.Now.AddDays(-1);
            //    //   res.Cookies.Add(aCookie);
            //}
        }

        //[DllImport("wininet.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)]
        //public static extern bool InternetSetOption(int hInternet, int dwOption, IntPtr lpBuffer, int dwBufferLength);
        //public static  unsafe void SuppressWininetBehavior()
        //{
      
        //    int option = (int)3/* INTERNET_SUPPRESS_COOKIE_PERSIST*/;
        //    int* optionPtr = &option;
        //    bool success = InternetSetOption(0, 81/*INTERNET_OPTION_SUPPRESS_BEHAVIOR*/, new IntPtr(optionPtr), sizeof(int));
        //    if (!success)
        //    {
        //      //  MessageBox.Show("Something went wrong !>?");
        //    }
        //}

        public static int  Timenum()
        {
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
            return (int)(DateTime.Now - startTime).TotalSeconds;
        }
    }
}
